package cn.gotom.core.web.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.gotom.coding.EncodingUtils;
import cn.gotom.core.pojos.Right;
import cn.gotom.core.service.RightService;
import cn.gotom.core.web.Constants;

import com.google.inject.Inject;

public class RightAction /* extends SupportAction */implements MenuInf
{

	public String list()
	{
		rightList = rightService.findAll();
		for (Right r : rightList)
		{
			EncodingUtils.encoding(r, "cp850", "GBK");
		}
		return Constants.SUCCESS;
	}

	public String input()
	{
		log.debug("input id=" + right.getId());
		right = rightService.get(right.getId());
		return Constants.INPUT;
	}

	public String remove()
	{
		try
		{
			log.debug("destroy  " + this);
			if (ids != null)
			{
				log.debug("remove id=" + ids.length + ids);
				for (String id : ids)
				{
					log.debug("remove id=" + id);
					if (rightService.exists(id))
					{
						rightService.remove(id);
					}
				}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			log.error(ex.getMessage(), ex);
		}
		return Constants.LIST;
	}

	public String save()
	{
		log.debug("input id=" + rightService.exists(right.getId()));
		EncodingUtils.encoding(right, "GBK", "cp850");
		right = rightService.save(right);
		return Constants.LIST;
	}

	protected final Logger log = Logger.getLogger(getClass());

	@Inject
	private RightService rightService;

	private String[] ids;

	private Right right;

	private List<Right> rightList = new ArrayList<Right>();

	@Override
	public String getName()
	{
		return "功能列表";
	}

	@Override
	public String getGroupName()
	{
		return "系统管理";
	}

	public String[] getIds()
	{
		return ids;
	}

	public void setIds(String[] ids)
	{
		this.ids = ids;
	}

	public Right getRight()
	{
		return right;
	}

	public void setRight(Right right)
	{
		this.right = right;
	}

	public List<Right> getRightList()
	{
		return rightList;
	}

	public void setRightList(List<Right> rightList)
	{
		this.rightList = rightList;
	}

	@Override
	public int getMenuSort()
	{
		return 10003;
	}

}
